<template>
	<view class="">
		<u-sticky bgColor="#fff">
			<u-tabs @change="sectionChange" :current="current" :list="list"  lineWidth="30"  lineColor="rgb(224, 31, 31)"
				itemStyle="padding:0 25px;height: 35px;" style="width: 100vw;" :inactiveStyle="{
		     color: '#606266',
		     transform: 'scale(1)'
		 }" :activeStyle="{
			 color: 'rgb(224, 31, 31)'
		 }"></u-tabs>
		</u-sticky>

		<view class="sub_lease_box" v-if="subLieaseList.length != 0">
			<view class="sub_lease_children" v-for="(item, index) of subLieaseList" :key="index" @click="detail(item)">
				<List :info="item" @closeLease="closeLease" :state="current" @againLease="againLease($event, item)"></List>
			</view>
			<u-loadmore :status="status" style="margin-top: 20rpx;" />
		</view>

		<u-empty v-else mode="data" icon="http://cdn.uviewui.com/uview/empty/data.png"></u-empty>
		<u-modal :show="makeShow" :title="title" :content='content' :closeOnClickOverlay="true" showCancelButton
			@confirm="confirmLease" @cancel="makeShow = false"></u-modal>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import {
		getSublet,
		putSubLease,
		putSublet,
		getUnreadNum,
		haveRead,
		sublet
	} from "@/api/my/index.js"

	import List from "./mycomponent/list.vue"
	export default {
		data() {
			return {
				title: '取消转租？',
				content: "",
				showModal: false,
				makeShow: false,
				status: 'loadmore',
				list:[{
						name: '待确认',
						id: 0,
					},
					{
						name: '已同意',
						id: 1,
						badge: {
							value: 0,
						}
					},
					{
						name: '已取消',
						id: 2
					},
					{
						name: '已拒绝',
						id: 3,
						badge: {
							value: 0,
						}
					},
				],
				current: 0,
				subLieaseList: [],
				pageNum: 1,
				subLeaseId: 0,
			}
		},
		components: {
			List
		},
		onLoad() {
			this.getSubLease()
		},
		onShow() {
			
		},
		methods: {
			async confirmLease() {
				const res = await putSubLease({
					id: this.subLeaseId,
					subletApplyState: 2
				})
				this.makeShow = false
				if (res.data.code != 200) return this.$refs.uToast.show({
					type: "error",
					message: res.data.msg
				})
				this.$refs.uToast.show({
					type: "success",
					message: '取消转租成功'
				})

				this.getSubLease()
			},
			closeLease(id) {
				this.makeShow = true;
				this.subLeaseId = id;
				this.showModal = true
			},
			detail(item){
				uni.navigateTo({
					url: './subletDetail?item='+ JSON.stringify(item),
					animationType: this.animation.type,
					animationDuration: this.animation.time
				})
			},
			async againLease(id, item){
				const res = await putSublet({
						id: item.id,
						subletApplyState: 0
					})
					if (res.data.code != 200) return this.$refs.uToast.show({
						type: "error",
						message: res.data.msg
					})
					this.$refs.uToast.show({
						type: "success",
						message:res.data.msg
					})
				
					this.getSubLease()
				},
			sectionChange(item) {
				this.current = item.id;
				this.getSubLease()
			},
			//获取角标数量（角标类型）
			async UnreadNum(informationType){
				//3,房屋转让申請已同意
				const res = await getUnreadNum(informationType);
				if (res.data.code != 200) return this.$refs.uToast.show({
					type: "error",
					message: res1.data.msg
				})
				//已同意消息未读数
				if(informationType == 3){
					this.list[1].badge.value = res.data.data.count;
				}
				//已拒绝消息未读数
				if(informationType == 4){
					this.list[3].badge.value = res.data.data.count;
				}
			},
			//获取列表
			async getSubLease(){
				//基础列表
				const res = await getSublet(this.pageNum, this.current);
				if (res.data.code != 200) return this.$refs.uToast.show({
					type: "error",
					message: res.data.msg
				})
				this.subLieaseList = res.data.rows;
				if (this.subLieaseList.length >= res.data.total) {
					this.status = 'nomore'
				}
				//获取申請已同意角标数量
				this.UnreadNum(3)
				//获取申请已拒绝角标数量
				this.UnreadNum(4)
				//用户点击已同意列表，把已同意列表未读的设置为已读
				if(this.current == 1){
					if(this.list[1].badge.value > 0){
						await haveRead({informationType: 3})
					}
				}
				//用户点击已拒绝列表，把已拒绝列表未读的设置为已读
				if(this.current == 3){
					if(this.list[3].badge.value > 0){
						await haveRead({informationType: 4})
					}
				}
			}
			
		}
	}
</script>

<style scoped>
	.sub_lease_box {
		padding: 0 20rpx;
		box-sizing: border-box;
	}
</style>